<?php
namespace App\Aspect;

use App\Job\RequestLogJob;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Utils\Context;
use Psr\Container\ContainerInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\DbConnection\Db;

/**
 * @Aspect
 */
class LogAspect extends AbstractAspect
{
    // 要切入的类，可以多个，亦可通过 :: 标识到具体的某个方法，通过 * 可以模糊匹配
    public $classes = [
        //管理后台--渠道模块
        /*'App\Controller\Admin\ChannelController::add',
        'App\Controller\Admin\ChannelController::edit',
        'App\Controller\Admin\ChannelController::del',
        'App\Controller\Admin\ChannelController::remark',
        'App\Controller\Admin\ChannelController::areaEdit',
        'App\Controller\Admin\ChannelController::areaBatchEdit',
        'App\Controller\Admin\ChannelController::areaBatchAllEdit',
        'App\Controller\Admin\ChannelController::rechargeAmount',
        //管理后台--金额模块
        'App\Controller\Admin\AmountController::typeAdd',
        'App\Controller\Admin\AmountController::typeEdit',
        'App\Controller\Admin\AmountController::typeDel',
        'App\Controller\Admin\AmountController::amountAdd',
        'App\Controller\Admin\AmountController::amountEdit',
        'App\Controller\Admin\AmountController::amountForbid',
        'App\Controller\Admin\AmountController::amountDel',
        'App\Controller\Admin\AmountController::channelEdit',
        //管理后台--优惠券模块
        'App\Controller\Admin\CouponController::generateCoupon',
        'App\Controller\Admin\CouponController::batchEdit',
        'App\Controller\Admin\CouponController::batchForbid',
        'App\Controller\Admin\CouponController::batchDel',
        //管理后台--用户模块
        'App\Controller\Admin\WxuserController::userEdit',
        'App\Controller\Admin\WxuserController::userDelte',
        'App\Controller\Admin\WxserviceController::edit',
        'App\Controller\Admin\WxserviceController::delete',
        'App\Controller\Admin\WxserviceController::typeEdit',
        'App\Controller\Admin\WxserviceController::typeAdd',
        'App\Controller\Admin\WxserviceController::typeDelte',
        'App\Controller\Admin\WxnoticeController::edit',
        'App\Controller\Admin\WxnoticeController::add',
        'App\Controller\Admin\WxnoticeController::delete',
        //管理后台--统计数据
        'App\Controller\Admin\WxvisitController::visithistory',
        //小程序API
        'App\Controller\Api\WxuserController::loginnew',
        'App\Controller\Api\WxuserController::login',
        'App\Controller\Api\WxuserController::sign',
        'App\Controller\Api\WxuserController::record',
        'App\Controller\Api\WxserviceController::fromMsg',
        'App\Controller\Api\WxuserController::logininfo',
        'App\Controller\Api\WxuserController::record',
        'App\Controller\Api\WxuserController::addsmall',
        //小程序接口--抽奖
        'App\Controller\Api\PrizeController::lottery',
        //小程序接口--分享优惠券
        'App\Controller\Api\CouponController::receive',
        //管理后台
        'App\Controller\Admin\ManageController::add',
        'App\Controller\Admin\ManageController::edit',*/
        // 'App\Controller\*Controller::edit',
        // 'App\Controller\*Controller::del',
        // 'App\Controller\ModemPool\MedemPhoneController::getPhone',
        // 'App\Controller\ModemPool\MedemPhoneController::getContent',
        // 'App\Controller\ModemPool\*Controller::add',
        // 'App\Controller\ModemPool\*Controller::edit',
        // 'App\Controller\ModemPool\*Controller::del',
        // 'App\Controller\ModemPool\*Controller::addRegular',
        // 'App\Controller\ModemPool\*Controller::editRegular',
        // 'App\Controller\ModemPool\*Controller::delRegular',
        // 'App\Controller\ModemPool\*Controller::settleBatch',
        // 'App\Controller\ModemPool\*Controller::withdrawAdd',
        // 'App\Controller\ModemPool\*Controller::withdrawTransfer',
    ];

    // 要切入的注解，具体切入的还是使用了这些注解的类，仅可切入类注解和类方法注解
    public $annotations = [
        
    ];

    protected $request;
    protected $container;

    public function __construct(RequestInterface $request, ContainerInterface $container){
        $this->request = $request;
        $this->container = $container;
    }

    public function process(ProceedingJoinPoint $proceedingJoinPoint)
    {
        // 切面切入后，执行对应的方法会由此来负责
        // $proceedingJoinPoint 为连接点，通过该类的 process() 方法调用原方法并获得结果
        // 在调用前进行某些处理
        $result = $proceedingJoinPoint->process();
        $params = $this->request->all();
        $path = $this->request->path();
        $user = Context::get('jwt_token') ;

        $data = [
            'path' => $path,
            'parameter' => json_encode($params),
            'result' => json_encode(json_decode($result, true)),
            'visit_id' => '',
        ];
        if(!empty($user)){
            $data['visit_id'] = $user['iss'] == 1 ? $user['uid'] : $user['account'];
        }
        //$driver = $this->container->get(DriverFactory::class)->get('request_log');
        //$driver->push(new RequestLogJob($data));

        // 在调用后进行某些处理
        return $result;
    }
}